#include "widget.h"
#include "ui_widget.h"
#include <QLine>
#include <QDebug>
#include <QAction>
#include <QPixmap>
#include <QLineEdit>
#include <QVBoxLayout>
#include <QGraphicsOpacityEffect>
Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    // QPushButton *button1 = new QPushButton("up",this);
    // QPushButton *button2 = new QPushButton("down",this);

    // button1->move(200, 300);
    // button2->move(300, 400);
    // connect(button1, &QPushButton::clicked, this, QOverload<>::of(&Widget::UpOpacity));
    // connect(button2, &QPushButton::clicked, this, QOverload<>::of(&Widget::DownOpacity));

    // connect(button1, &QPushButton::clicked, this, [this, button1](){
    //     UpOpacity(button1);
    // });
    // connect(button2, &QPushButton::clicked, this, [this, button2](){
    //     DownOpacity(button2);
    // });

    // QGraphicsOpacityEffect *effect = new QGraphicsOpacityEffect(button1);
    // effect->setOpacity(0.5); // 50% 透明
    // button1->setGraphicsEffect(effect);


    // EmitSignals();

    // 设置鼠标触发样式
    // QPixmap pixmap("D:/c盘图片/make.png");
    // pixmap = pixmap.scaled(16, 16);//设置图片大小

    // QCursor cursor(pixmap, 2, 2);//设置触发区域
    // // ui->pushButton->setCursor(QCursor(pixmap, 4, 2));
    // ui->pushButton->setCursor(cursor);

    // // 设置提示文字及其间隔(ms)
    // ui->pushButton->setToolTip("这是ui文件创建的按钮");
    // ui->pushButton->setToolTipDuration(5000);

    // QLineEdit *line = new QLineEdit();
    // line->move(100,200);


    // 创建一个Line Edit，ui文件创建比较方便
    // QVBoxLayout *layout = new QVBoxLayout(this);

    // QLineEdit *lineEdit = new QLineEdit(this);
    // lineEdit->setPlaceholderText("请输入内容...");

    // layout->addWidget(lineEdit);
    // setLayout(layout);

    // connect(ui->pushButton, &QPushButton::clicked, this, &Widget::on_pushButton_light_clicked);
    // connect(ui->pushButton_2, &QPushButton::clicked, this, &Widget::on_pushButton_dark_clicked);
    // QIcon icon(":/make.png");
    // QPushButton *button1 = new QPushButton("button",this);
    // button1->setIcon(icon);
    // button1->setIconSize(QSize(50,50));

    ui->pushButton_target->setIcon(QIcon(":/make.png"));
    ui->pushButton_target->setIconSize(QSize(100, 100));
    ui->pushButton_up->setIcon(QIcon(":/up.png"));
    ui->pushButton_down->setIcon(QIcon(":/down.png"));
    ui->pushButton_left->setIcon(QIcon(":/left.png"));
    ui->pushButton_right->setIcon(QIcon(":/right.png"));

    // ui->pushButton_up->setShortcut(QKeySequence(Qt::Key_W));
    // ui->pushButton_down->setShortcut(QKeySequence(Qt::Key_S));
    // ui->pushButton_left->setShortcut(QKeySequence(Qt::Key_A));
    // ui->pushButton_right->setShortcut(QKeySequence(Qt::Key_D));
    QAction *moveup = new QAction(this);
    QList<QKeySequence> shortcuts;
    shortcuts << QKeySequence("w");
    shortcuts << QKeySequence("up");
    moveup->setShortcuts(shortcuts);
    this->addAction(moveup);
    connect(moveup, &QAction::triggered, ui->pushButton_up, &QPushButton::click);
    connect(moveup, &QAction::triggered, this, &Widget::on_pushButton_up_clicked);
    // ui->pushButton_up->setShortcut(QKeySequence("w"));
    // ui->pushButton_up->setShortcut(QKeySequence("up"));
    ui->pushButton_down->setShortcut(QKeySequence("a"));
    ui->pushButton_left->setShortcut(QKeySequence("s"));
    ui->pushButton_right->setShortcut(QKeySequence("d"));
}

void Widget::UpOpacity()
{
    double opacity = this->windowOpacity();
    if (opacity >= 1.0)
    {
        return;
    }
    qDebug() << "opacity" << opacity;
    opacity += 0.1;
    this->setWindowOpacity(opacity);
}

void Widget::DownOpacity()
{
    double opacity = this->windowOpacity();
    if (opacity <= 0.0)
    {
        return;
    }
    qDebug() << "opacity" << opacity;
    opacity -= 0.1;
    this->setWindowOpacity(opacity);
}

void Widget::UpOpacity(QPushButton *button)
{
    double opacity = button->windowOpacity();
    if (opacity >= 1.0)
    {
        return;
    }
    qDebug() << "opacity" << opacity;
    opacity += 0.1;
    button->setWindowOpacity(opacity);
}

void Widget::DownOpacity(QPushButton *button)
{
    double opacity = button->windowOpacity();
    if (opacity <= 0.0)
    {
        return;
    }
    qDebug() << "opacity" << opacity;
    opacity -= 0.1;
    button->setWindowOpacity(opacity);
}

// void Widget::EmitSignals()
// {

// }

Widget::~Widget()
{
    delete ui;
}


// void Widget::on_pushButton_dark_clicked()
// {
//     this->setStyleSheet("background-color: #333");
//     ui->textEdit->setStyleSheet("background-color: #333; color: #fff;");
//     ui->pushButton->setStyleSheet("color: #fff");
//     ui->pushButton_2->setStyleSheet("color: #fff");
// }


// void Widget::on_pushButton_light_clicked()
// {
//     this->setStyleSheet("background-color: #f3f3f3");
//     ui->textEdit->setStyleSheet("background-color: #fff; color: #000;");
//     ui->pushButton->setStyleSheet("color: #000");
//     ui->pushButton_2->setStyleSheet("color: #000");
// }


void Widget::on_pushButton_up_clicked()
{
    const QRect& rect = ui->pushButton_target->geometry();
    ui->pushButton_target->setGeometry(rect.x(), rect.y() - 5, rect.width(),
                                       rect.height());
    qDebug() << "up";
}


void Widget::on_pushButton_left_clicked()
{
    const QRect& rect = ui->pushButton_target->geometry();
    ui->pushButton_target->setGeometry(rect.x() - 5, rect.y(), rect.width(),
                                       rect.height());
    qDebug() << "left";
}


void Widget::on_pushButton_down_clicked()
{
    const QRect& rect = ui->pushButton_target->geometry();
    ui->pushButton_target->setGeometry(rect.x(), rect.y() + 5, rect.width(),
                                       rect.height());
    qDebug() << "down";
}


void Widget::on_pushButton_right_clicked()
{
    const QRect& rect = ui->pushButton_target->geometry();
    ui->pushButton_target->setGeometry(rect.x() + 5, rect.y(), rect.width(),
                                       rect.height());
    qDebug() << "right";
}

